import cv2
import easyocr
import numpy as np

img = cv2.imread("../images/warp_text.png")
cv2.imshow("warp_text", img)

# 进行透视变化
# 1. 获取透视变换矩阵
# 原图像的四个点
src = np.array([
    [138, 118],
    [635, 3],
    [635, 143],
    [181, 229]], dtype="f4"
)
# 目标图像的四个点
dst = np.array([
    [170, 22],
    [645, 32],
    [646, 180],
    [190, 150]
], dtype="f4")

M = cv2.getPerspectiveTransform(src, dst)

h, w, _ = img.shape
# 进行变换
per_img = cv2.warpPerspective(img, M, (w, h))
cv2.imshow("per_img", per_img)

cv2.waitKey(0)

reader = easyocr.Reader(
    ['ch_sim', 'en'],
    model_storage_directory=r'C:\Users\systems\Desktop\仓库\shang-hai_ai\w2_图像认知与OpenCV\day 2\model',
    gpu=False,
    download_enabled=False  # 禁用在线下载
)

# 2. 读取图像并识别
results = reader.readtext(per_img)

print(results)  # list []
plate_text = "".join([res[1] + "\n" for res in results])
print(plate_text)
